// This script provides a way to view essentially technical tracks from within
// the session overview view for Silicon Valley Code Camp. It changes the
// links on the topics in the topic tag cloud to the right of the session
// overview listing to make them highlight just the sessions on the left that
// have the selected topic associated with them.
//
// version 1.0.3
// 2009-09-28
// Copyright (c) 2008, Silicon Valley Code Camp
// Released under the MIT license
// http://www.opensource.org/licenses/mit-license.php
//
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script.
//
// To install, you need Greasemonkey: http://greasemonkey.mozdev.org/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts,
// select "Silicon Valley Code Camp", and click Uninstall.
// --------------------------------------------------------------------
//
// ==UserScript==
// @name          Silicon Valley Code Camp
// @namespace     http://www.siliconvalley-codecamp.com
// @description   Adds session highlighting by topic on the session schedule page.
// @include       http://siliconvalley-codecamp.com/*
// @include       http://www.siliconvalley-codecamp.com/*
// ==/UserScript==

// --------------------------------------------------------------------
// Helper Functions

function vanbox_getDocumentNodes(query) {
    return document.evaluate(query, document, null,
            XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
            null);
}

function vanbox_highlightSessions(event) {
    var query = "//table[@class='contentTable']//td[@class='contentMain']//table//td/span/ul/li/a";
    var sessionNodes = vanbox_getDocumentNodes(query);

    if (sessionNodes) {
        var topic = event.target.textContent;
        if (topic.search(/\u00a0/) != -1) {
            // Deal with non-breaking spaces in the topic tag cloud tags
            topic = topic.replace(/\u00a0/g, " ");
        }
        for (var i = 0; i < sessionNodes.snapshotLength; i++) {
            var node = sessionNodes.snapshotItem(i);
            if (node.parentNode.getAttribute("style") == "background-color: yellow;") {
                node.parentNode.setAttribute("style", "background-color: white;");
            }
            var topicValue = node.getAttribute("topic");
            if (topicValue) {
                var topics = topicValue.split(",");
                for (j = 0; j < topics.length; j++) {
                    if (topics[j] == topic) {
                        node.parentNode.setAttribute("style", "background-color: yellow;");
                        break;
                    }
                }
            }
        }
    }
};

function vanbox_modifyTopicTagCloud() {
    var query = "//div[@class='cloudControl']//td[@class='dxccControl']/span/a";
    var topicNodes = vanbox_getDocumentNodes(query);

    if (topicNodes) {
        var hrefValue = "javascript:void(0);";
        for (var i = 0; i < topicNodes.snapshotLength; i++) {
            var node = topicNodes.snapshotItem(i);
            node.setAttribute("href", hrefValue);

            // NOTE: Must use event listeners to bind functions to later
            //       actions on the page like clicking tags in the
            //       topic tag cloud.
            node.addEventListener("click", vanbox_highlightSessions, true);
        }
    }

}

function vanbox_isSessionsOverviewPage() {
    var result = false;

    if (  window.location.host == "siliconvalley-codecamp.com"
       || window.location.host == "www.siliconvalley-codecamp.com") {

        var pageRegExp = /SessionsOverview.aspx$/;
        if (window.location.href.search(pageRegExp) != -1) {
            result = true;
        }
    }

    return result;
}

// --------------------------------------------------------------------
// Apply only to Sessions Overview page on the camp site

if (vanbox_isSessionsOverviewPage()) {

    // Rewrite the tag cloud links to call vanbox_highlightSessions
    vanbox_modifyTopicTagCloud();
    
}

